class MainActivity : AppCompatActivity() {
    private lateinit var transferManager: TransferManager
    private lateinit var resumableTransfer: ResumableTransfer
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        setupTransferManager()
        observeTransfers()
    }

    private fun setupTransferManager() {
        transferManager = TransferManager(this)
        resumableTransfer = ResumableTransfer(this, transferManager)
        
        transferManager.transferProgress.observe(this) { info ->
            updateProgressUI(info)
            
            when (info.status) {
                TransferStatus.PAUSED -> {
                    resumableTransfer.saveTransferState(info.fileId, info.transferredSize)
                }
                TransferStatus.COMPLETED -> {
                    resumableTransfer.clearTransferState(info.fileId)
                    verifyTransfer(info)
                }
                TransferStatus.ERROR -> {
                    showError("传输失败")
                }
                else -> { /* 处理其他状态 */ }
            }
        }
    }

    private fun verifyTransfer(info: TransferInfo) {
        lifecycleScope.launch {
            val file = File(getExternalFilesDir(null), info.fileName)
            val isValid = ChecksumCalculator().verifyChecksum(file, info.checksum)
            
            if (isValid) {
                showSuccess("传输完成")
            } else {
                showError("文件校验失败")
            }
        }
    }
} 